package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.RegionMapper;
import cn.wolfcode.luowowo.article.query.RegionQueryObject;
import cn.wolfcode.luowowo.article.service.IRegionService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
public class RegionServiceImpl implements IRegionService {
    @Autowired
    private RegionMapper regionMapper;

    @Autowired
    private DestinationMapper destinationMapper;

    @Override
    public PageInfo queryForPageInfo(RegionQueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());

        return new PageInfo(regionMapper.queryForList(qo));
    }

    @Override
    public void saveOrUpdate(Region region) {
        if (region != null) {
            if (region.getId() == null) {
                regionMapper.insert(region);

            } else {
                regionMapper.updateByPrimaryKey(region);
            }

        }
    }

    @Override
    public void changeHotValue(int hot, Long id) {

        if (id != null) {
            regionMapper.changeHotValue(hot, id);
        }

    }

    @Override
    public void delete(Long id) {
        if (id != null) {
            regionMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public List<Destination> getDestByRegionId(Long rid) {

        Region region = regionMapper.selectByPrimaryKey(rid);

        Long[] refIds = region.getRefIds();

        List<Destination> destinations = destinationMapper.queryDestinationsByRefIds(refIds);

        return destinations;
    }

    @Override
    public List<Region> queryHotRegion() {

        return regionMapper.queryHotRegion();

    }

    @Override
    public List<Destination> getHotDestByRegionId(Long regionId) {

        List<Destination> destinations = new ArrayList<Destination>();

        //-1为中国
        if (regionId != null) {
            if (regionId == -1L) {

                destinations = destinationMapper.getHotDestByParentId(1L);
            } else {

                Region region = regionMapper.selectByPrimaryKey(regionId);

                Long[] refIds = region.getRefIds();

                destinations = destinationMapper.getHotDestByRegionId(refIds);
            }

        }
        return destinations;
    }
}
